/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.templates;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.lineage.L1DatabaseFactory;
import com.lineage.server.model.L1Location;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.utils.collections.Lists;

/**
 * Class <code>L1DoorSpawn</code> 数据库模版:创造门(把门放到游戏内).
 * 
 * @author jrwz
 * @version 2012-6-16上午09:05:32
 * @see com.lineage.server.templates
 * @since JDK1.7
 */
public final class L1DoorSpawn {

    /** 提示信息. */
    private static final Logger LOG = Logger.getLogger(L1DoorSpawn.class
            .getName());

    /**
     * 从数据库中读取所有门并插入集合中.
     * 
     * @return 集合内的结果
     */
    public static List<L1DoorSpawn> all() {
        final List<L1DoorSpawn> result = Lists.newArrayList();
        Connection con = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            con = L1DatabaseFactory.getInstance().getConnection();
            pstm = con.prepareStatement("SELECT * FROM spawnlist_door");
            rs = pstm.executeQuery();
            while (rs.next()) {
                final int id = rs.getInt("id");
                final int gfxId = rs.getInt("gfxid");
                final int x = rs.getInt("locx");
                final int y = rs.getInt("locy");
                final int mapId = rs.getInt("mapid");
                final int hp = rs.getInt("hp");
                final int keeper = rs.getInt("keeper");
                final boolean isOpening = rs.getBoolean("isOpening");
                final L1DoorGfx gfx = L1DoorGfx.findByGfxId(gfxId);
                final L1DoorSpawn spawn = new L1DoorSpawn(id, gfx, x, y, mapId,
                        hp, keeper, isOpening);
                result.add(spawn);
            }

        } catch (final SQLException e) {
            LOG.log(Level.SEVERE, e.getLocalizedMessage(), e);
        } finally {
            SQLUtil.close(rs);
            SQLUtil.close(pstm);
            SQLUtil.close(con);
        }
        return result;
    }

    /** 门的编号. */
    private final int doorId;
    /** 门的图形. */
    private final L1DoorGfx doorGfx;
    /** 门所在的坐标:X. */
    private final int doorX;
    /** 门所在的坐标:Y. */
    private final int doorY;
    /** 门所在的地图编号. */
    private final int doorMapId;
    /** 坐标点/位置. */
    private final L1Location loc;
    /** 门的血量. */
    private final int doorHp;
    /** 门的管理者(负责开门的NPC). */
    private final int doorKeeper;
    /** 门的状态:是否打开. */
    private final boolean doorIsOpening;

    /**
     * 数据库模版:创造门(把门放到游戏内).
     * 
     * @param id
     *            门的编号
     * @param gfx
     *            门的图形
     * @param x
     *            门所在的坐标:X
     * @param y
     *            门所在的坐标:Y
     * @param mapId
     *            门所在的地图编号
     * @param hp
     *            门的血量
     * @param keeper
     *            门的管理者(负责开门的NPC)
     * @param isOpening
     *            门的状态:是否打开
     */
    public L1DoorSpawn(final int id, final L1DoorGfx gfx, final int x,
            final int y, final int mapId, final int hp, final int keeper,
            final boolean isOpening) {
        super();
        this.doorId = id;
        this.doorGfx = gfx;
        this.doorX = x;
        this.doorY = y;
        this.doorMapId = mapId;
        this.loc = new L1Location(this.doorX, this.doorY, this.doorMapId);
        this.doorHp = hp;
        this.doorKeeper = keeper;
        this.doorIsOpening = isOpening;
    }

    /**
     * 取得门的图形.
     * 
     * @return 门的图形
     */
    public L1DoorGfx getGfx() {
        return this.doorGfx;
    }

    /**
     * 取得门的血量.
     * 
     * @return 门的血量
     */
    public int getHp() {
        return this.doorHp;
    }

    /**
     * 取得门的编号.
     * 
     * @return 门的编号
     */
    public int getId() {
        return this.doorId;
    }

    /**
     * 取得门的管理者(负责开门的NPC).
     * 
     * @return 门的管理者(负责开门的NPC)
     */
    public int getKeeper() {
        return this.doorKeeper;
    }

    /**
     * 取得坐标点/位置.
     * 
     * @return 坐标点/位置
     */
    public L1Location getLocation() {
        return this.loc;
    }

    /**
     * 取得门所在的地图编号.
     * 
     * @return 门所在的地图编号
     */
    public int getMapId() {
        return this.doorMapId;
    }

    /**
     * 取得门所在的坐标:X.
     * 
     * @return 门所在的坐标:X
     */
    public int getX() {
        return this.doorX;
    }

    /**
     * 取得门所在的坐标:Y.
     * 
     * @return 门所在的坐标:Y
     */
    public int getY() {
        return this.doorY;
    }

    /**
     * 取得门的状态:是否打开.
     * 
     * @return 门的状态:是否打开
     */
    public boolean isOpening() {
        return this.doorIsOpening;
    }
}
